package com.ruoyi.web.controller.document;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.DocFileManage;
import com.ruoyi.common.core.domain.entity.DocTemp;
import com.ruoyi.common.core.domain.entity.DocTempManage;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.vo.TempVo;
import com.ruoyi.system.service.DocTempService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@Api(tags = "项目模板管理")
@RestController
@RequestMapping("/document/temp")
public class DocTempController extends BaseController {

    @Autowired
    private DocTempService docTempService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询项目模板信息列表
     */
    @PreAuthorize("@ss.hasPermi('document:template:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询项目模板信息列表")
    public TableDataInfo list(DocTempManage docTempManage) {

        startPage();
        List<TempVo> list = docTempService.selectDocTempManageList(docTempManage);
        return getDataTable(list);
    }

    /**
     * 修改项目模板信息
     */
    @ApiOperation(value = "修改项目模板信息")
    @PreAuthorize("@ss.hasPermi('document:template:update')")
    @Log(title = "修改项目模板信息", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody DocTempManage docTempManage) {
        return toAjax(docTempService.updateDocTempManage(docTempManage));
    }


    /**
     * 逻辑删除项目模板信息
     */
    @ApiOperation(value = "逻辑删除项目模板信息")
    @PreAuthorize("@ss.hasPermi('document:template:delete')")
    @Log(title = "逻辑删除项目模板信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tempId}")
    public AjaxResult remove(@PathVariable Long[] tempId) {
        return toAjax(docTempService.deleteDocTempManageByTempIds(tempId));
    }


    /**
     * 新增项目模板信息
     */
    @ApiOperation(value = "新增项目模板信息")
    @PreAuthorize("@ss.hasPermi('document:template:add')")
    @Log(title = "新增项目模板信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DocTempManage docTempManage) {

        docTempManage.setTempUploader(getUsername());
        return toAjax(docTempService.insertDocTempManage(docTempManage));
    }


    /**
     * 上传请求
     */
    @ApiOperation(value = "实现文件上传")
    @Log(title = "文件上传", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('document:template:upload')")
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            String name = file.getOriginalFilename();
            DocTemp docTemp = new DocTemp();
            docTemp.setTempName(name);
            docTemp.setTempPath(filePath);
            docTemp.setTempUnique(fileName);
            docTemp.setUrl(url);
            Long fileId = docTempService.insertPathName(docTemp);
            ajax.put("fileId", fileId);
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;

        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}